package mymac.com.weather;

import android.os.Handler;
import android.os.Message;
import android.util.Log;

import org.json.JSONObject;

/**
 * Created by mymac on 2017/3/3.
 */

public class GetData {
    private static GetData getData = null;

    public static GetData getInstance(){
        if (getData == null){
            getData = new GetData();
        }
        return getData;
    }

//    public static void getData(String address){
//        HttpRequest.getInstance().getRequest(address, new CallBack() {
//            @Override
//            public void getJson(JSONObject jsonObject) {
//                JSONObject json = jsonObject;
//                getWeatherData(json);
//
//                JSONObject resultJson = json.optJSONObject("result");
//                Log.e("-----resultJson---->",resultJson.toString());
//
//                JSONObject sk = resultJson.optJSONObject("sk");
//                Log.e("--------sk-------->",sk.toString());
//                getSkData(sk);
//
//                JSONObject today = resultJson.optJSONObject("today");
//                Log.e("-------today------->",today.toString());
//                getTodayData(today);
//
//                JSONObject futureJson = resultJson.optJSONObject("future");
//                Log.e("-----futureJson----->",futureJson.toString());
//
//            }
//        });
//        HttpRequest.getInstance().getRequest(address,new Handler(){
//            @Override
//            public void handleMessage(Message msg) {
//                super.handleMessage(msg);
//                JSONObject json = (JSONObject) msg.obj;
//                Log.e("-----json----->",json.toString());
//                getWeatherData(json);
//
//                JSONObject resultJson = json.optJSONObject("result");
//                Log.e("-----resultJson---->",resultJson.toString());
//
//                JSONObject sk = resultJson.optJSONObject("sk");
//                Log.e("--------sk-------->",sk.toString());
//                getSkData(sk);
//
//                JSONObject today = resultJson.optJSONObject("today");
//                Log.e("-------today------->",today.toString());
//                getTodayData(today);
//
//                JSONObject futureJson = resultJson.optJSONObject("future");
//                Log.e("-----futureJson----->",futureJson.toString());
//
//            }
//        });
//    }

    private static void getWeatherData(JSONObject jsonObject){
        Weather.getInstance().resultcode = jsonObject.optString("resultcode");
        Weather.getInstance().reason = jsonObject.optString("reason");
        Weather.getInstance().error_code = jsonObject.optString("error_code");
    }

    private static void getTodayData(JSONObject jsonObject) {
        Today.getInstance().city = jsonObject.optString("city");
        Today.getInstance().comfort_index = jsonObject.optString("comfort_index");
        Today.getInstance().temperature = jsonObject.optString("temperature");
        Today.getInstance().weather = jsonObject.optString("weather");
        Today.getInstance().wind = jsonObject.optString("wind");
        Today.getInstance().week = jsonObject.optString("week");
        Today.getInstance().date_y = jsonObject.optString("date_y");
        Today.getInstance().dressing_index = jsonObject.optString("dressing_index");
        Today.getInstance().dressing_advice = jsonObject.optString("dressing_advice");
        Today.getInstance().uv_index = jsonObject.optString("uv_index");
        Today.getInstance().wash_index = jsonObject.optString("wash_index");
        Today.getInstance().travel_index = jsonObject.optString("travel_index");
        Today.getInstance().exercise_index = jsonObject.optString("exercise_index");
        Today.getInstance().drying_index = jsonObject.optString("drying_index");
        //Today.getInstance().weather_id.fa = jsonObject.optJSONObject("weather_id").optString("fa");
        //Today.getInstance().weather_id.fb = jsonObject.optJSONObject("weather_id").optString("fb");
    }

    private static void getSkData(JSONObject jsonObject) {
        Sk.getInstance().temp = jsonObject.optString("temp");
        Sk.getInstance().humidity = jsonObject.optString("humidity");
        Sk.getInstance().time = jsonObject.optString("time");
        Sk.getInstance().wind_strength = jsonObject.optString("wind_strength");
        Sk.getInstance().wind_direction = jsonObject.optString("wind_direction");
    }
}
